// pages/mine/meeting.js
import {
  get
} from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    triggeredRefresh: false,
    pageOpt: {
      pageNum: 1,
      pageSize: 5,
      userSearchContent: ""
    },
    pageTotal: 0,
    rows: [],
    loadMoreText: "点击加载更多",
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      "pageOpt.pageNum": 1,
      "pageOpt.userSearchContent": ''
    })
    this.pullData(true)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  onRefresh() {
    this.pullData()
  },

  onLoadMore(e) {
    this.pullData()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      "pageOpt.pageNum": 1,
      "pageOpt.userSearchContent": ''
    })
    this.pullData(true)
  },

  toDetail(e) {
    const {
      detail
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/mine/mDetail',
      success: function (params) {
        params.eventChannel.emit("meetingData", detail)
      }
    })
  },

  pullData(init = false) {
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true,
      loadMoreText: '数据加载中'
    })
    const that = this;
    get("/user/receipt/list", {
      ...that.data.pageOpt
    }).then(res => {
      const rows = res.rows || []
      let total = res.total || 0
      if (total != 0) {
        total = Math.ceil(total / that.data.pageOpt.pageSize)
      }

      if (that.data.pageOpt.pageNum > total) {
        if (that.data.rows.length == rows.length) {
          wx.showToast({
            title: '没有更多数据了',
            icon: 'none'
          })
          that.setData({
            triggeredRefresh: false,
            pageTotal: total,
            loading: false,
            loadMoreText: "没有更多数据了"
          })
          return
        } else {
          init = true
        }
      }

      if (rows.length > 0) {
        rows.forEach(row => {

          let action = row.meeting.publicActions
          if (action) {
            try {
              let item = JSON.parse(action)
              if (item.constructor !== Array) {
                item.name = item.name.substr(0, 2)
                row.meeting.publicActions = [item]
              } else {
                item.forEach(p => {
                  p.name = p.name.substr(0, 2)
                })
                row.meeting.publicActions = item
              }
            } catch (error) {
              row.meeting.publicActions = []
            }
          }

          let imagestr = row.meeting.coverImage
          if (imagestr && imagestr.includes(",")) {
            row.meeting.coverImage = imagestr.split(",")
            return
          }
          if (imagestr) {
            row.meeting.coverImage = [imagestr]
            return
          }
          row.meeting.coverImage = []
        });
      }
      if (that.data.rows.length > 0 && !init) {
        rows.push(...that.data.rows)
      }
      let pageNum = that.data.pageOpt.pageNum + 1
      let text = '点击加载更多'
      if (rows.length < that.data.pageOpt.pageSize) {
        text = "没有更多数据了"
      }
      that.setData({
        triggeredRefresh: false,
        "pageOpt.pageNum": pageNum,
        pageTotal: total,
        loadMoreText: text,
        loading: false,
        rows
      })
    }).catch(err => {
      wx.showToast({
        title: '网络异常,请稍后再试',
        icon: 'none'
      })
      that.setData({
        triggeredRefresh: false,
        loading: false,
        loadMoreText: '网络异常'
      })
    })
  },
})